package com.ruoyi.radar.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.radar.common.CommonUtil;
import com.ruoyi.radar.domain.RadarSilo;
import com.ruoyi.radar.service.RadarSiloService;
import com.ruoyi.radar.mapper.RadarSiloMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
* @author BD
* @description 针对表【radar_silo】的数据库操作Service实现
* @createDate 2024-12-05 16:52:55
*/
@Service
public class RadarSiloServiceImpl extends ServiceImpl<RadarSiloMapper, RadarSilo>
    implements RadarSiloService{

    @Autowired
    private RadarSiloMapper radarSiloMapper;

    @Autowired
    private RadarSiloService radarSiloService;

    @Autowired
    protected Validator validator;

    @Autowired
    CommonUtil commonUtil;

    @Override
    public List<RadarSilo> selectSiloList(Map<String, Object> param) {

        if(!"1".equals(param.get("type"))){
            commonUtil.toPage(param);
        }
        List<RadarSilo> radarSilos = radarSiloMapper.selectSiloList(param);

        return radarSilos;
    }

    @Override
    public Integer getCount(Map<String, Object> param) {

        Integer count = radarSiloMapper.getCount(param);

        return count;
    }

    @Override
    public List<RadarSilo> selectSiloWithConfList(Map<String, Object> param) {

        return radarSiloMapper.selectSiloWithConfList(param);
    }

    @Override
    public String importSilo(List<RadarSilo> siloList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(siloList) || siloList.size() == 0)
        {
            throw new ServiceException("导入料仓数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (RadarSilo radarSilo : siloList)
        {
            try
            {
                // 验证是否存在这个点位
                HashMap<String, Object> map = new HashMap<>();
                map.put("siloStockId",radarSilo.getSiloStockId());

                List<RadarSilo> radarSilos = radarSiloMapper.selectSiloList(map);
//                if (StringUtils.isNull(gnssPoint))
                if (radarSilos.size() == 0)
                {
                    BeanValidators.validateWithException(validator, user);
//                    radarSilo.setDcsEquipId(commonUtil.uuId());
                    radarSilo.setCreateTime(commonUtil.currentTime());
                    radarSilo.setUpdateTime(commonUtil.currentTime());

                    radarSiloService.save(radarSilo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号 " + radarSilo.getSiloStockId() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
//                    radarSilo.setSiloStockId(commonUtil.checkNull(GnssRecieveEquips.get(0).getDcsEquipId()));
                    radarSilo.setUpdateTime(commonUtil.currentTime());
                    radarSiloService.updateById(radarSilo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号 " + radarSilo.getSiloStockId() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编号 " + radarSilo.getSiloStockId() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编号 " + radarSilo.getSiloStockId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}




